#ifndef __WOE_SERVICE_IMP_S___
#define __WOE_SERVICE_IMP_S___

#include "Service.h"

//
//	Server Data Interface
//
class TCOM_API ServiceData
{
public:
	virtual ~ServiceData(){}
};

//
//	Service Implementation
//	Base on boost thread and asio
//
class TCOM_API ServiceImp : public Service
{
public:
	ServiceImp();

	//
	//	initialize service
	//	using this function to initialize some static data
	//	this function should not have too much logic that consuming too much time
	//	large scale data initialize should be in onStart which will be runed in 
	//	separated threads
	//
	virtual void init(ServiceData& data);

	//
	//	server start and stop
	//
	virtual void start();
	virtual void stop();
	virtual void wait();

	//
	//	post an message to current thread
	//
	virtual void postMsg(IMsg* pMsg);
protected:
	//
	//	main run loop for current service
	//
	virtual void run();

	//
	//	invoke by service just after its starting
	//	invoke by service just before its stopping
	virtual void onStart();
	virtual void onStop();

	//
	//	callbacks
	//
	virtual void onTimer();
	virtual void onDispatchMsg(IMsg* pMsg);

protected:
	boost::asio::io_service			m_theIoSvc;
	boost::asio::io_service::work	m_theEmptyWork;
	boost::asio::deadline_timer		m_theTimmer;
	boost::asio::strand				m_theStrand;
	boost::thread					m_theThreads;
	boost::posix_time::seconds		m_theUpdateCycle;

	bool							m_bContinueRunning;
};

#endif

